package com.youzan.cloud.component.biz.member.youzanopen.api;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youzan.cloud.base.api.BifrostService;
import com.youzan.cloud.component.base.util.core.ObjectUtil;
import com.youzan.cloud.component.biz.member.common.enums.LevelEnum;
import com.youzan.cloud.component.biz.member.common.util.LogRecordUtil;
import com.youzan.cloud.component.biz.member.common.util.OutResultUtil;
import com.youzan.cloud.component.biz.member.common.util.TokenUtil;
import com.youzan.cloud.component.biz.member.config.MemberLinkerConfiguration;
import com.youzan.cloud.component.biz.member.dal.dataobject.MemberInformation;
import com.youzan.cloud.component.biz.member.dal.dataobject.MemberRelationship;
import com.youzan.cloud.component.biz.member.dal.dataobject.MerchantConfiguration;
import com.youzan.cloud.component.biz.member.dal.mapper.MemberInformationMapper;
import com.youzan.cloud.component.biz.member.dal.mapper.MemberRelationshipMapper;
import com.youzan.cloud.component.biz.member.dal.mapper.MerchantConfigurationMapper;
import com.youzan.cloud.component.biz.member.dto.BaseResult;
import com.youzan.cloud.component.biz.member.dto.MemberBaseDto;
import com.youzan.cloud.component.biz.member.dto.linker.out.MemberGradeLinkerResult;
import com.youzan.cloud.component.biz.member.dto.linker.out.MemberLinkerBaseResult;
import com.youzan.cloud.component.biz.member.dto.linker.out.MemberQueryResult;
import com.youzan.cloud.connect.service.common.domain.BizServiceInfo;
import com.youzan.cloud.connect.service.sdk.ConnectServiceCaller;
import com.youzan.cloud.connect.service.sdk.ConnectServiceResponse;
import com.youzan.cloud.connect.service.sdk.exception.ConnectServiceCallException;
import com.youzan.cloud.open.sdk.common.exception.SDKException;
import com.youzan.cloud.open.sdk.core.client.auth.Token;
import com.youzan.cloud.open.sdk.core.client.core.DefaultYZClient;
import com.youzan.cloud.open.sdk.core.client.core.YouZanClient;
import com.youzan.cloud.open.sdk.gen.v1_0_1.api.YouzanScrmCustomerDetailGet;
import com.youzan.cloud.open.sdk.gen.v1_0_1.model.YouzanScrmCustomerDetailGetParams;
import com.youzan.cloud.open.sdk.gen.v1_0_1.model.YouzanScrmCustomerDetailGetResult;
import com.youzan.cloud.open.sdk.gen.v2_0_0.api.YouzanCrmCustomerGrowthDecrease;
import com.youzan.cloud.open.sdk.gen.v2_0_0.api.YouzanCrmCustomerGrowthGet;
import com.youzan.cloud.open.sdk.gen.v2_0_0.api.YouzanCrmCustomerGrowthIncrease;
import com.youzan.cloud.open.sdk.gen.v1_0_0.api.YouzanUsersInfoQuery;
import com.youzan.cloud.open.sdk.gen.v2_0_0.model.*;
import com.youzan.cloud.open.sdk.gen.v1_0_0.model.YouzanUsersInfoQueryParams;
import com.youzan.cloud.open.sdk.gen.v1_0_0.model.YouzanUsersInfoQueryResult;
import com.youzan.cloud.open.sdk.gen.v3_0_0.api.YouzanScrmCustomerGet;
import com.youzan.cloud.open.sdk.gen.v3_0_0.model.YouzanScrmCustomerGetParams;
import com.youzan.cloud.open.sdk.gen.v3_0_0.model.YouzanScrmCustomerGetResult;
import com.youzan.cloud.open.sdk.gen.v3_0_0.model.YouzanScrmCustomerUpdateParams;
import com.youzan.cloud.open.sdk.gen.v3_0_0.model.YouzanScrmCustomerUpdateResult;
import com.youzan.cloud.open.sdk.gen.v3_0_0.api.YouzanScrmCustomerUpdate;
import com.youzan.cloud.open.sdk.gen.v4_0_0.api.YouzanScrmCustomerLevelSet;
import com.youzan.cloud.open.sdk.gen.v4_0_0.model.YouzanScrmCustomerLevelSetParams;
import com.youzan.cloud.open.sdk.gen.v4_0_0.model.YouzanScrmCustomerLevelSetResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.Map;
import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;

/**
 * @author liling
 */
@Component
@Slf4j
public class YzMemberApiService {

    @Autowired
    TokenUtil tokenUtil;

    @Autowired
    BifrostService bifrostService;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    MemberRelationshipMapper memberRelationshipMapper;

    @Autowired
    MerchantConfigurationMapper merchantConfigurationMapper;

    @Autowired
    MemberLinkerConfiguration memberLinkerConfiguration;

    @Autowired
    ConnectServiceCaller connectServiceCaller;

    @Autowired
    LogRecordUtil logRecordUtil;

    @Autowired
    MemberInformationMapper memberInformationMapper;

    @Autowired
    YzMemberApiService yzMemberApiService;

    public YouzanUsersInfoQueryResult.YouzanUsersInfoQueryResultData queryResultData(String yzOpenId, String kdtId){
        YouzanUsersInfoQuery youzanUsersInfoQuery = new YouzanUsersInfoQuery();
        Token accessToken = tokenUtil.getToken(kdtId);
        YouzanUsersInfoQueryParams youzanUsersInfoQueryParams = new YouzanUsersInfoQueryParams();
        youzanUsersInfoQueryParams.setYzOpenId(yzOpenId);
        youzanUsersInfoQuery.setAPIParams(youzanUsersInfoQueryParams);
        try {
            YouzanUsersInfoQueryResult result = bifrostService.invoke(youzanUsersInfoQuery,accessToken, YouzanUsersInfoQueryResult.class);
            if(ObjectUtil.isNotNull(result) && ObjectUtil.isNotNull(result.getData()) && result.getSuccess()){
               return result.getData();
            }
        } catch (SDKException e) {
            log.error("调用api获取用户信息报错",e);
            if("4203".equals(e.getCode().toString())){
                YouzanUsersInfoQueryResult apiResult = tokenUtil.getApiResult(youzanUsersInfoQuery, YouzanUsersInfoQueryResult.class, kdtId);
                return apiResult.getData();
            }
        }
        return null;
    }


    public BaseResult  getMemberLevel(String yzOpenId, String kdtId, MemberBaseDto memberBaseDto){
        if(ObjectUtil.isEmpty(memberBaseDto.getMemberGradeDto())){
            return OutResultUtil.fail("查询等级参数为空","500",null);
        }
        BizServiceInfo memberQueryLinker = memberLinkerConfiguration.getMemberGradeQueryLinker();
        ConnectServiceResponse<MemberLinkerBaseResult> gradeMemberResult = null;
        try{
            gradeMemberResult = connectServiceCaller.callConnectService(memberQueryLinker.getConnectServiceName(),memberQueryLinker.getTargetSystem(),memberQueryLinker.getTargetSystemVersion(),memberBaseDto.getMemberGradeDto());
        }catch (ConnectServiceCallException e) {
            log.error("调用三方会员等级查询接口失败" + e.getMessage());
            return OutResultUtil.fail(gradeMemberResult.getErrMsg(),gradeMemberResult.getErrorCode().toString(),gradeMemberResult);
        }
        if(gradeMemberResult.isSuccess()){
            MemberGradeLinkerResult responseDTO = (MemberGradeLinkerResult) gradeMemberResult.getResponseDTO();
            if(responseDTO!=null){
                        initUserGrade(responseDTO.getGradeId(),yzOpenId,kdtId);
                return OutResultUtil.success(gradeMemberResult.getResponseDTO());
            }
        }
        return OutResultUtil.fail(gradeMemberResult.getErrMsg(),gradeMemberResult.getErrorCode().toString(),gradeMemberResult);
    }

    public void initUserGrade(String cardLevel,String yzOpenId,String kdtId){
        // 上锁
        String keyPrefix = "initUserGrade";
        String key = keyPrefix + yzOpenId;
        if(!redisTemplate.opsForValue().setIfAbsent(key, "1", LevelEnum.LEVEL_REDIS_TIME, TimeUnit.MILLISECONDS)){
            return;
        }
        try{
            log.info("开始设置会员等级");
            initMemberLevel(cardLevel,yzOpenId,kdtId);
        } catch (Exception e){
            log.info("设置会员等级失败");
        } finally {
            redisTemplate.delete(key);
        }
    }

    private void adjustUserGradeAmount(String yzOpenId,Map maptype,String cno,String kdtId){
        Integer currentGradeAmount = null;
        Integer newGradeAmount = null;
         currentGradeAmount = yzMemberApiService.getYouzanCrmCustomerGrowthGetResult(kdtId, yzOpenId);
        //获取到新的成长值
        Object o = maptype.get(cno);
        if(ObjectUtil.isEmpty(o)){
            return;
        }
        newGradeAmount = Integer.parseInt(o.toString());
        if(currentGradeAmount == null){
            currentGradeAmount = 0;
        }
        if(currentGradeAmount>newGradeAmount){
            decrease(yzOpenId,currentGradeAmount-newGradeAmount,kdtId);
        } else if(currentGradeAmount<newGradeAmount){
            increase(yzOpenId,newGradeAmount-currentGradeAmount,kdtId);
        }
        MemberInformation memberInformation = new MemberInformation();
        memberInformation.setYzOpenid(yzOpenId);
        memberInformation.setGrowth(newGradeAmount);
        memberInformationMapper.updateByYzOpenId(memberInformation);
    }

    private void increase(String yzOpenId, int amount,String kdtId){
        long start = System.currentTimeMillis();
        // 获取Access Token
        Token accessToken = null;
        try {
            accessToken = tokenUtil.getToken(kdtId);
        } catch (Exception e) {
            log.error("获取token报错",e);
        }
        YouzanCrmCustomerGrowthIncrease youzanCrmCustomerGrowthIncrease = new YouzanCrmCustomerGrowthIncrease();
        //创建参数对象,并设置参数
        YouzanCrmCustomerGrowthIncreaseParams youzanCrmCustomerGrowthIncreaseParams = new YouzanCrmCustomerGrowthIncreaseParams();
        YouzanCrmCustomerGrowthIncreaseParams.YouzanCrmCustomerGrowthIncreaseParamsParams param = new YouzanCrmCustomerGrowthIncreaseParams.YouzanCrmCustomerGrowthIncreaseParamsParams();
        param.setReason("手工调整会员等级");
        param.setAmount(amount);
        YouzanCrmCustomerGrowthIncreaseParams.YouzanCrmCustomerGrowthIncreaseParamsUser  youzanCrmCustomerGrowthIncreaseUserParams = new YouzanCrmCustomerGrowthIncreaseParams.YouzanCrmCustomerGrowthIncreaseParamsUser();
        youzanCrmCustomerGrowthIncreaseUserParams.setAccountType(5);
        youzanCrmCustomerGrowthIncreaseUserParams.setAccountId(yzOpenId);
        //param.setBizValue(UUID.randomUUID().toString());
        param.setUser(youzanCrmCustomerGrowthIncreaseUserParams);
        youzanCrmCustomerGrowthIncreaseParams.setParams(param);
        youzanCrmCustomerGrowthIncrease.setAPIParams(youzanCrmCustomerGrowthIncreaseParams);
        YouzanCrmCustomerGrowthIncreaseResult result = null;
        try {
            result = bifrostService.invoke(youzanCrmCustomerGrowthIncrease, accessToken, YouzanCrmCustomerGrowthIncreaseResult.class);
        } catch (SDKException e) {
            log.error("增加会员成长值失败");
            if("4203".equals(e.getCode().toString())){
                tokenUtil.getApiResult(youzanCrmCustomerGrowthIncrease, YouzanCrmCustomerGrowthIncreaseResult.class, kdtId);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("增加会员成长值耗时============" + (end-start));
        System.out.println(JSON.toJSON(result));
    }

    private void decrease(String yzOpenId, int amount,String kdtId){
        long start = System.currentTimeMillis();
        // 获取Access Token
        Token accessToken = null;
        try {
            accessToken = tokenUtil.getToken(kdtId);
        } catch (Exception e) {
            log.error("获取token报错",e);
        }
        YouzanCrmCustomerGrowthDecrease youzanCrmCustomerGrowthDecrease = new YouzanCrmCustomerGrowthDecrease();
        //创建参数对象,并设置参数
        YouzanCrmCustomerGrowthDecreaseParams youzanCrmCustomerGrowthDecreaseParams = new YouzanCrmCustomerGrowthDecreaseParams();
        YouzanCrmCustomerGrowthDecreaseParams.YouzanCrmCustomerGrowthDecreaseParamsParams param = new YouzanCrmCustomerGrowthDecreaseParams.YouzanCrmCustomerGrowthDecreaseParamsParams();
        param.setReason("手工调整会员等级");
        param.setAmount(amount);
        YouzanCrmCustomerGrowthDecreaseParams.YouzanCrmCustomerGrowthDecreaseParamsUser  youzanCrmCustomerGrowthDecreaseUserParams = new YouzanCrmCustomerGrowthDecreaseParams.YouzanCrmCustomerGrowthDecreaseParamsUser();
        youzanCrmCustomerGrowthDecreaseUserParams.setAccountType(5);
        youzanCrmCustomerGrowthDecreaseUserParams.setAccountId(yzOpenId);
        //param.setBizValue(UUID.randomUUID().toString());
        param.setUser(youzanCrmCustomerGrowthDecreaseUserParams);
        youzanCrmCustomerGrowthDecreaseParams.setParams(param);
        youzanCrmCustomerGrowthDecrease.setAPIParams(youzanCrmCustomerGrowthDecreaseParams);
        YouzanCrmCustomerGrowthIncreaseResult result = null;
        try {
            result = bifrostService.invoke(youzanCrmCustomerGrowthDecrease, accessToken, YouzanCrmCustomerGrowthIncreaseResult.class);
        } catch (SDKException e) {
            log.error("增加会员成长值失败",e);
            if("4203".equals(e.getCode().toString())){
                tokenUtil.getApiResult(youzanCrmCustomerGrowthDecrease, YouzanCrmCustomerGrowthIncreaseResult.class, kdtId);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("扣减会员成长值============" + (end-start));
        System.out.println(JSON.toJSON(result));
    }

    public Boolean updateMemberInformation(MemberInformation memberInformation,String kdtId){
        Token token = tokenUtil.getToken(kdtId);
        YouzanScrmCustomerUpdate youzanScrmCustomerUpdate = new YouzanScrmCustomerUpdate();
        //创建参数对象,并设置参数
        YouzanScrmCustomerUpdateParams youzanScrmCustomerUpdateParams = new YouzanScrmCustomerUpdateParams();
        YouzanScrmCustomerUpdateParams.YouzanScrmCustomerUpdateParamsAccount  youzanScrmCustomerUpdateParamsCustomerUpdate = new YouzanScrmCustomerUpdateParams.YouzanScrmCustomerUpdateParamsAccount();
        YouzanScrmCustomerUpdateParams.YouzanScrmCustomerUpdateParamsCustomerupdate youzanScrmCustomerUpdateParamsCustomerupdate = new YouzanScrmCustomerUpdateParams.YouzanScrmCustomerUpdateParamsCustomerupdate();
        YouzanScrmCustomerUpdateParams.YouzanScrmCustomerUpdateParamsContactaddress youzanScrmCustomerUpdateParamsContactaddress = new YouzanScrmCustomerUpdateParams.YouzanScrmCustomerUpdateParamsContactaddress();
        youzanScrmCustomerUpdateParamsCustomerupdate.setGender(Short.parseShort(memberInformation.getGender()));
        youzanScrmCustomerUpdateParamsCustomerupdate.setBirthday(memberInformation.getBirthday()==null?null:memberInformation.getBirthday());
        youzanScrmCustomerUpdateParamsCustomerupdate.setName(memberInformation.getSecondName()==null?null:memberInformation.getSecondName());
       // youzanScrmCustomerUpdateParamsCustomerupdate.setWeiXin(memberInformation.getWeiXin()==null?null:youzanScrmCustomerUpdateParamsCustomerupdate.getWeiXin());
        youzanScrmCustomerUpdateParamsCustomerUpdate.setAccountId(memberInformation.getYzOpenid());
        youzanScrmCustomerUpdateParamsCustomerUpdate.setAccountType("YzOpenId");
        youzanScrmCustomerUpdateParamsContactaddress.setAddress(memberInformation.getAddress()==null?null:memberInformation.getAddress());
        youzanScrmCustomerUpdateParamsContactaddress.setAreaCode(memberInformation.getAreaCode()==null?null:memberInformation.getAreaCode());
        youzanScrmCustomerUpdateParamsCustomerupdate.setContactAddress(youzanScrmCustomerUpdateParamsContactaddress);
        youzanScrmCustomerUpdateParams.setAccount(youzanScrmCustomerUpdateParamsCustomerUpdate);
        youzanScrmCustomerUpdateParams.setCustomerUpdate(youzanScrmCustomerUpdateParamsCustomerupdate);
        youzanScrmCustomerUpdate.setAPIParams(youzanScrmCustomerUpdateParams);
        try {
            YouzanScrmCustomerUpdateResult result = bifrostService.invoke(youzanScrmCustomerUpdate, token, YouzanScrmCustomerUpdateResult.class);
            if(ObjectUtil.isNotNull(result) && ObjectUtil.isNotNull(result.getData()) && result.getSuccess()){
                return result.getSuccess();
            }
        }catch (SDKException e){
            log.error("调用有赞会员更新API接口异常" + e.getMessage());
            if("4203".equals(e.getCode().toString())){
                YouzanScrmCustomerUpdateResult apiResult = tokenUtil.getApiResult(youzanScrmCustomerUpdate, YouzanScrmCustomerUpdateResult.class, kdtId);
                return apiResult.getSuccess();
            }
        }
          return  false;
    }

    public YouzanScrmCustomerDetailGetResult.YouzanScrmCustomerDetailGetResultData getYouzanScrmCustomerDetailGetResultData(String kdtId, String yzOpenId){
        Token token = tokenUtil.getToken(kdtId);
        YouzanScrmCustomerDetailGet youzanScrmCustomerDetailGet = new YouzanScrmCustomerDetailGet();
       //创建参数对象,并设置参数
        YouzanScrmCustomerDetailGetParams youzanScrmCustomerDetailGetParams = new YouzanScrmCustomerDetailGetParams();
        youzanScrmCustomerDetailGetParams.setYzOpenId(yzOpenId);
        youzanScrmCustomerDetailGetParams.setFields("user_base,tags,benefit_cards,benefit_level,benefit_rights,credit,behavior,giftcard,prepaid,coupon,level,auth_info");
        youzanScrmCustomerDetailGet.setAPIParams(youzanScrmCustomerDetailGetParams);
        try {
            YouzanScrmCustomerDetailGetResult  result = bifrostService.invoke(youzanScrmCustomerDetailGet, token, YouzanScrmCustomerDetailGetResult.class);
            if(ObjectUtil.isNotNull(result) && ObjectUtil.isNotNull(result.getData())){
                 return result.getData();
             }
        }catch (SDKException e){
            log.error("调用有赞会员查询API接口异常" + e.getMessage());
            if("4203".equals(e.getCode().toString())){
                YouzanScrmCustomerDetailGetResult apiResult = tokenUtil.getApiResult(youzanScrmCustomerDetailGet, YouzanScrmCustomerDetailGetResult.class, kdtId);
                return apiResult.getData();
            }
        }
          return null;
    }

    public YouzanScrmCustomerGetResult.YouzanScrmCustomerGetResultData getYouzanScrmCustomerGetResultData(String kdtId, String mobile){
        Token token = tokenUtil.getToken(kdtId);
        YouzanScrmCustomerGet youzanScrmCustomerGet = new YouzanScrmCustomerGet();
        //创建参数对象,并设置参数
        YouzanScrmCustomerGetParams youzanScrmCustomerGetParams = new YouzanScrmCustomerGetParams();
        youzanScrmCustomerGetParams.setMobile(mobile);
        youzanScrmCustomerGetParams.setFansType(Short.valueOf("1"));
        youzanScrmCustomerGetParams.setFansId(0L);
        youzanScrmCustomerGet.setAPIParams(youzanScrmCustomerGetParams);
        try {
            YouzanScrmCustomerGetResult result = bifrostService.invoke(youzanScrmCustomerGet, token, YouzanScrmCustomerGetResult.class);
             if(ObjectUtil.isNotNull(result) && ObjectUtil.isNotNull(result.getData())){
                 return result.getData();
             }
        }catch (SDKException e){
            log.error("调用有赞获取会员概要API接口异常" + e.getMessage());
            if("4203".equals(e.getCode().toString())){
                YouzanScrmCustomerGetResult apiResult = tokenUtil.getApiResult(youzanScrmCustomerGet, YouzanScrmCustomerGetResult.class, kdtId);
                return apiResult.getData();
            }
        }
        return null;
    }

    public void setPropertiesInformation(MemberInformation memberInformation,MemberQueryResult memberQueryResult,MemberBaseDto memberBaseDto,YouzanUsersInfoQueryResult.YouzanUsersInfoQueryResultData userInfo,
                                         YouzanScrmCustomerDetailGetResult.YouzanScrmCustomerDetailGetResultData youzanScrmCustomerDetailGetResultData){
        String gender = youzanScrmCustomerDetailGetResultData == null || youzanScrmCustomerDetailGetResultData.getGender() == null?"0":youzanScrmCustomerDetailGetResultData.getGender().toString();
        String avatar = userInfo == null || userInfo.getLatestInfo() == null || userInfo.getLatestInfo().getAvatar() == null ? "" : userInfo.getLatestInfo().getAvatar();
        String secondName = youzanScrmCustomerDetailGetResultData == null || youzanScrmCustomerDetailGetResultData.getName() == null ?"":youzanScrmCustomerDetailGetResultData.getName();
        String lastNickName =  userInfo == null || userInfo.getLatestInfo() == null || userInfo.getLatestInfo().getNickName() == null ? "" : userInfo.getLatestInfo().getNickName();
        String birthday = youzanScrmCustomerDetailGetResultData == null || youzanScrmCustomerDetailGetResultData.getBirthday() == null ? "" : youzanScrmCustomerDetailGetResultData.getBirthday();
        Integer area = null;
        memberInformation.setGender(memberQueryResult.getGender()==null?gender:memberQueryResult.getGender());
        memberInformation.setKdtid(Long.valueOf(memberBaseDto.getKdtId()));
        memberInformation.setLatestAvatar(memberQueryResult.getLatestAvatar()==null?avatar:memberQueryResult.getLatestAvatar());
        memberInformation.setYzOpenid(memberBaseDto.getYzOpenId());
        memberInformation.setSecondName(memberQueryResult.getLatestNickName()==null?secondName:memberQueryResult.getLatestNickName());
        memberInformation.setLatestNickName(memberQueryResult.getLatestNickName()==null?lastNickName:memberQueryResult.getLatestNickName());
        memberInformation.setGrade(memberQueryResult.getGradeId()==null?null:memberQueryResult.getGradeId());
        memberInformation.setAge(memberQueryResult.getAge()==null?null:memberQueryResult.getAge());
        memberInformation.setFirstName(memberQueryResult.getFirstName()==null?null:memberQueryResult.getFirstName());
        memberInformation.setBirthday(memberQueryResult.getBirthday()==null?birthday:memberQueryResult.getBirthday());
        memberInformation.setEmail(memberQueryResult.getEmail()==null?null:memberQueryResult.getEmail());
        memberInformation.setUpdateTime(new Date());
        memberInformation.setThirdAccount(memberQueryResult.getThirdAccount()==null?null:memberQueryResult.getThirdAccount());
        memberInformation.setThirdAccountType(memberQueryResult.getThirdAccountType()==null?"1":memberQueryResult.getThirdAccountType());
        memberInformation.setAreaCode(area);
        memberInformation.setAddress(buildAddress(youzanScrmCustomerDetailGetResultData));
    }

    public String buildAddress(YouzanScrmCustomerDetailGetResult.YouzanScrmCustomerDetailGetResultData resultData){
        if(ObjectUtil.isNull(resultData)){
            return null;
        }
        StringJoiner stringJoiner = new StringJoiner("-");
        if(StringUtils.isNotBlank(resultData.getProvinceName())){
            stringJoiner.add(resultData.getProvinceName());
        }
        if(StringUtils.isNotBlank(resultData.getCityName())){
            stringJoiner.add(resultData.getCityName());
        }if(StringUtils.isNotBlank(resultData.getCountyName())){
            stringJoiner.add(resultData.getCountyName());
        }
        return stringJoiner.toString().length() >  0 ? stringJoiner.toString():null;
    }

    /**
     * @description: 查询会员当前成长值
     * @author ts
     * @date
     * @version 1.0
     */
     public Integer  getYouzanCrmCustomerGrowthGetResult(String kdtId,String yzOpenId){
         Token token = tokenUtil.getToken(kdtId);
         YouzanCrmCustomerGrowthGet youzanCrmCustomerGrowthGet = new YouzanCrmCustomerGrowthGet();
         //创建参数对象,并设置参数
         YouzanCrmCustomerGrowthGetParams youzanCrmCustomerGrowthGetParams = new YouzanCrmCustomerGrowthGetParams();
         YouzanCrmCustomerGrowthGetParams.YouzanCrmCustomerGrowthGetParamsParams youzanCrmCustomerGrowthGetParamsParams  = new YouzanCrmCustomerGrowthGetParams.YouzanCrmCustomerGrowthGetParamsParams();
         YouzanCrmCustomerGrowthGetParams.YouzanCrmCustomerGrowthGetParamsUser youzanCrmCustomerGrowthGetParamsUser  = new YouzanCrmCustomerGrowthGetParams.YouzanCrmCustomerGrowthGetParamsUser();
         youzanCrmCustomerGrowthGetParamsUser.setAccountType(5);
         youzanCrmCustomerGrowthGetParamsUser.setAccountId(yzOpenId);
         youzanCrmCustomerGrowthGetParamsParams.setUser(youzanCrmCustomerGrowthGetParamsUser);
         youzanCrmCustomerGrowthGetParams.setParams(youzanCrmCustomerGrowthGetParamsParams);
         youzanCrmCustomerGrowthGet.setAPIParams(youzanCrmCustomerGrowthGetParams);
         try {
             YouzanCrmCustomerGrowthGetResult result = bifrostService.invoke(youzanCrmCustomerGrowthGet, token, YouzanCrmCustomerGrowthGetResult.class);
             if(ObjectUtil.isNotNull(result) && ObjectUtil.isNotNull(result.getData())){
                 return Integer.parseInt(result.getData().getGrowth().toString());
             }
         }catch (SDKException e){
             log.error("调用有赞查询会员当前成长值API接口异常" + e.getMessage());
             if("4203".equals(e.getCode().toString())){
                 YouzanCrmCustomerGrowthGetResult apiResult = tokenUtil.getApiResult(youzanCrmCustomerGrowthGet, YouzanCrmCustomerGrowthGetResult.class, kdtId);
                 return Integer.parseInt(apiResult.getData().getGrowth().toString());
             }
         }
         return null;
     }

     /**
      * @description: 设置会员等级
      * @author
      * @date
      * @version 1.0
      */
     public void initMemberLevel(String cardLevel, String yzOpenId, String kdtId) {
         //获取等级映射关系 更新有赞
         MerchantConfiguration merchantConfiguration = merchantConfigurationMapper.selectAll(LevelEnum.LEVEL_MAPPING);
         JSONObject jsonObject = JSON.parseObject(merchantConfiguration.getConfigValue());
         String levelAlias = jsonObject.getString(cardLevel);
         Token token = tokenUtil.getToken(kdtId);
         YouzanScrmCustomerLevelSet youzanScrmCustomerLevelSet = new YouzanScrmCustomerLevelSet();
         YouzanScrmCustomerLevelSetParams youzanScrmCustomerLevelSetParams = new YouzanScrmCustomerLevelSetParams();
         YouzanScrmCustomerLevelSetParams.YouzanScrmCustomerLevelSetParamsParams youzanScrmCustomerLevelSetParamsParams  = new YouzanScrmCustomerLevelSetParams.YouzanScrmCustomerLevelSetParamsParams();
         youzanScrmCustomerLevelSetParams.setParams(youzanScrmCustomerLevelSetParamsParams);
         youzanScrmCustomerLevelSetParamsParams.setLevelAlias(levelAlias);
         YouzanScrmCustomerLevelSetParams.YouzanScrmCustomerLevelSetParamsUser youzanScrmCustomerLevelSetParamsUser  = new YouzanScrmCustomerLevelSetParams.YouzanScrmCustomerLevelSetParamsUser();
         youzanScrmCustomerLevelSetParamsParams.setUser(youzanScrmCustomerLevelSetParamsUser);
         youzanScrmCustomerLevelSetParamsUser.setAccountType(5);
         youzanScrmCustomerLevelSetParamsUser.setAccountId(yzOpenId);
         youzanScrmCustomerLevelSet.setAPIParams(youzanScrmCustomerLevelSetParams);
         try{
             YouzanScrmCustomerLevelSetResult result  = bifrostService.invoke(youzanScrmCustomerLevelSet, token, YouzanScrmCustomerLevelSetResult.class);
             log.info("调整会员等级成功! {} 会员等级为 ====== " + levelAlias);
         }catch (SDKException e){
             log.info("调整会员等级失败! {} msg " + e.getMessage());
             if("4203".equals(e.getCode().toString())){
                 tokenUtil.getApiResult(youzanScrmCustomerLevelSet, YouzanScrmCustomerLevelSetResult.class,kdtId );
             }
         }
     }

}
